package ua.yandex.shad.tempseries;

import static java.lang.StrictMath.*;

public class TemperatureSeriesAnalysis {

    private final double[] temperatureSeries;

    public TemperatureSeriesAnalysis(double[] temperatureSeries) throws IllegalTemperatureValueException {
        if (temperatureSeries != null) {
            for (double t : temperatureSeries) {
                if (t < (-273.0)) {
                    throw new IllegalTemperatureValueException();
                }
            }
        }
        this.temperatureSeries = temperatureSeries;
    }

    private boolean isEmpty() {
        return (temperatureSeries == null || temperatureSeries.length == 0);
    }

    public double calcAverageTemp() {
        if (isEmpty()) {
            return 0;
        }

        double sum = 0;
        for (double d : temperatureSeries) {
            sum += d;
        }
        return sum / temperatureSeries.length;
    }

    public double calcTempDeviation() {
        if (isEmpty()) {
            return 0;
        }

        double sum1 = 0;
        double sum2 = 0;
        for (double t : temperatureSeries) {
            sum1 += pow(t, 2);
            sum2 += t;
        }
        sum2 = (sum2 * sum2) / temperatureSeries.length;
        return (sum1 - sum2) / temperatureSeries.length;
    }

    public double findTempClosestToZero() {
        return findTempClosestToValue(0.0);
    }

    public double findTempClosestToValue(double tempValue) {
        if (isEmpty()) {
            return 0;
        }

        double delta = abs(tempValue - temperatureSeries[0]);
        double ans = temperatureSeries[0];
        for (int i = 1; i < temperatureSeries.length; i++) {
            if (abs(tempValue - temperatureSeries[i]) < delta) {
                ans = temperatureSeries[i];
                delta = abs(tempValue - temperatureSeries[i]);
            } else {
                if (abs(tempValue - temperatureSeries[i]) == delta && temperatureSeries[i] >= tempValue) {
                    ans = temperatureSeries[i];
                }
            }
        }
        return ans;

    }

    public double[] findTempsLessThen(double tempValue) {
        if (isEmpty()) {
            return new double[]{};
        }

        int n = 0;
        for (double t : temperatureSeries) {
            if (t < tempValue) {
                n++;
            }
        }

        double[] ans = new double[n];
        n = 0;
        for (double t : temperatureSeries) {
            if (t < tempValue) {
                ans[n++] = t;
            }
        }

        return ans;
    }

    public double[] findTempsGreaterThen(double tempValue) {
        if (isEmpty()) {
            return new double[]{};
        }
        int n = 0;
        for (double t : temperatureSeries) {
            if (t > tempValue) {
                n++;
            }
        }

        double[] ans = new double[n];
        n = 0;
        for (double t : temperatureSeries) {
            if (t > tempValue) {
                ans[n++] = t;
            }
        }

        return ans;
    }

    public double[] forecatsTemp(double tempValue) {
        return new double[]{};
    }
}
